package com.example.wishstar.view.adpter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wishstar.R;
import com.example.wishstar.databinding.HabitItemBinding;
import com.example.wishstar.databinding.WishItemBinding;
import com.example.wishstar.model.Habit;

import java.util.List;


public class HabitAdapter extends RecyclerView.Adapter<HabitAdapter.ViewHolder> {
    private String TAG = "HabitAdapter";
    private List<Habit> mHabitList;
    private Context mcontext;
    private OnHabitItemClickListener onHabitItemClickListener;

    public interface OnHabitItemClickListener {
        void onItemClick(Habit habit);
        void onLongItemClick(Habit habit);
    }

    public void setOnHabitItemClickListener(OnHabitItemClickListener onHabitItemClickListener) {
        this.onHabitItemClickListener = onHabitItemClickListener;
    }

    public HabitAdapter(List<Habit> mHabitList, Context mcontext) {
        this.mHabitList = mHabitList;
        this.mcontext = mcontext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        HabitItemBinding habitItemBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()), R.layout.habit_item, parent, false);

        return new ViewHolder(habitItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull HabitAdapter.ViewHolder holder, int position) {
        Habit habit = mHabitList.get(position);
        holder.habitItemBinding.setHabit(habit);
        if (habit.isDoneToday == 1) {
            TextView textView  = holder.habitItemBinding.habitItemIconImageBg;
            GradientDrawable gradientDrawable = (GradientDrawable) textView.getBackground();
            gradientDrawable.setColor(mcontext.getColor(R.color.green_light));
        }
        else {
            TextView textView  = holder.habitItemBinding.habitItemIconImageBg;
            GradientDrawable gradientDrawable = (GradientDrawable) textView.getBackground();
            gradientDrawable.setColor(mcontext.getColor(R.color.white));
        }



        switch (habit.habitIcon) {
            case 1:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_water));
                break;
            case 2:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_agriculture));
                break;
            case 3:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_apple));
                break;
            case 4:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_book));
                break;
            case 5:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_drink));
                break;
            case 6:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_economy));
                break;
            case 7:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_egg));
                break;
            case 8:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_listen));
                break;
            case 9:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_mask));
                break;
            case 10:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_pet));
                break;
            case 11:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_run));
                break;
            case 12:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_shopping));
                break;
            case 13:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_swimming));
                break;
            case 14:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_teeth));
                break;
            case 15:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_sun));
                break;
            case 16:
                holder.habitItemBinding.habitItemIconImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_agenda));
                break;
        }
        holder.habitItemBinding.habitItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                Log.e(TAG, "pos -> " + pos);
                onHabitItemClickListener.onItemClick(mHabitList.get(pos));
            }
        });
        holder.habitItemBinding.habitItemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                int pos = holder.getLayoutPosition();
                Log.e(TAG, "pos -> " + pos);
                initPopWindow(view, pos);
                return true;
            }
        });
    }

    private void initPopWindow(View v, int pos) {
        View view = LayoutInflater.from(mcontext).inflate(R.layout.memory_item_delete, null, false);
        TextView textView  = view.findViewById(R.id.btn_delete);
        //1.构造一个PopupWindow，参数依次是加载的View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画

        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题
        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效


        //设置popupWindow显示的位置，参数依次是参照View，x轴的偏移量，y轴的偏移量
        popWindow.showAsDropDown(v, 70, -60);

        //设置popupWindow里的按钮的事件
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onHabitItemClickListener.onLongItemClick(mHabitList.get(pos));
                Toast.makeText(mcontext, "你点击了删除", Toast.LENGTH_SHORT).show();
                popWindow.dismiss();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mHabitList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        HabitItemBinding habitItemBinding;

        public ViewHolder(@NonNull HabitItemBinding itemView) {
            super(itemView.getRoot());
            habitItemBinding = itemView;
        }
    }
}
